package jp.tkym.labs.gwt.server.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jp.tkym.labs.gwt.client.bean.GanttHeader;
import jp.tkym.labs.util.date.DateUtil;

class GanttHeaderFactory {
	private static final String NBSP = " ";
	
	List<GanttHeader> headerList(Date today, int span){
		Date start = DateUtil.addMonth(today, -1);
		Date end = DateUtil.addMonth(today, 1);
		return headerList(start, end, 1);
	}
	
	List<GanttHeader> headerList(Date start, Date end, int span){
		Date incr = start;
		Date tomorrowEndDate = DateUtil.addDay(end, 1);
		List<GanttHeader> list = new ArrayList<GanttHeader>();
		while(incr.before(tomorrowEndDate)){
			GanttHeader header = header(incr, span); 
			incr = DateUtil.addDay(incr, span);
			list.add(header);
		}
		return list;
	}

	GanttHeader header(Date start, int day){
		GanttHeader header = new GanttHeader();
		int date = DateUtil.toInt(start) % 100;
		String head = "";
		if(date < 10 && date>0) head = NBSP;
		header.setTitle(head+date);
		if(day > 1)header.setSpan(day);
		return header;
	}
}